package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.watson.R;
import com.watson.utils.SPUtils;
import com.watson.utils.StringUtils;

import java.util.ArrayList;


public class KuwrAdapter extends RecyclerView.Adapter<KuwrAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<String> mList;
    private String R_Auth_Code = "";

    public KuwrAdapter(Context mContext, ArrayList<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
        R_Auth_Code = SPUtils.get(mContext, "auth", "") + "";
    }

    public void setData(ArrayList<String> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_kuwr, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvname.setText(mList.get(position) + "");
            holder.tvstd.setText((position + 1) + "、");
            if (R_Auth_Code.contains("DelNotInWarehouse")) {
                holder.imgDel.setVisibility(View.VISIBLE);
            } else {
                holder.imgDel.setVisibility(View.GONE);
            }
            holder.imgDel.setOnClickListener(v -> {
                if (StringUtils.isFastClick()) {
                    return;
                }
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList.get(position));
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvname, tvstd;
        private ImageView imgDel;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvstd = itemView.findViewById(R.id.tvstd);
            tvname = itemView.findViewById(R.id.tvname);
            imgDel = itemView.findViewById(R.id.imgDel);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, String mList);
    }
}
